<?php

/**
 * Menashop
 *
 * LICENSE
 *
 * This source file is subject to the license that is bundled
 * with this project in the file LICENSE.txt
 *
 * @category   Zend
 * @package    Admin_Model
 * @subpackage Manufacturer
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 * @version    1.0.0
 * @since      File available since Release 1.0.0
 */

/**
 * The domain for manufacturer
 * 
 * @category   Zend
 * @package    Admin_Model
 * @subpackage Manufacturer
 * @copyright  Copyright (c) 2011 Menashop UA Inc. (http://www.menashop.com.ua)
 * @license    LICENSE.txt
 */
class Admin_Model_Manufacturer extends Zend_Db_Table_Abstract
{

    /**
     * The name of table
     * @var string 
     */
    protected $_name = 'manufacturer';

    /**
     * Save manufacturer
     * @param array $row 
     */
    public function save($row)
    {
        // check ID (insert or update)
        $id = isset($row['id']) ? $row['id'] : null;
        if ($id) {
            unset($row['id']);
            $this->update($row, array('id = ?' => $id));
        } else {
            $this->insert($row);
        }
    }

    /**
     * Find manufacturer ID by name
     * @param string $name
     * @return integer
     */
    public function findIdByName($name)
    {
        // find manufacturer by name in database
        $query = $this->getAdapter()->select();
        $query->from($this->_name, 'id');
        $query->where('name = ?', $name);
        $result = $this->getAdapter()->fetchOne($query);
        return $result;
    }

    /**
     * Find ID of manufacturer by content ID
     * @param integer $contentId
     * @return integer 
     */
    public function findIdByContentId($contentId)
    {
        // find manufacturer by Content ID in database
        $query = $this->getAdapter()->select();
        $query->from($this->_name, 'id');
        $query->where('content_id = ?', $contentId);
        $result = $this->getAdapter()->fetchOne($query);
        return $result;
    }

    /**
     * Fetch all manufacturer IDs and names
     * @return array 
     */
    public function fetchPairsNameAndContentId()
    {
        // fetch all IDs and names from database
        $query = $this->getAdapter()->select();
        $query->from($this->_name, array('UPPER(name)', 'content_id'));
        $query->where('content_sponsor = ?', 1);
        $result = $this->getAdapter()->fetchPairs($query);
        return $result;
    }

    /**
     * Fetch all manufacturer content IDs and IDs
     * @return array 
     */
    public function fetchPairsContentIdAndId()
    {
        // fetch all content IDs and IDs from database
        $query = $this->getAdapter()->select();
        $query->from($this->_name, array('content_id', 'id'));
        $query->where('content_sponsor = ?', 1);
        $result = $this->getAdapter()->fetchPairs($query);
        return $result;
    }

    /**
     * Find manufacturer IDs by name
     * @param string $name 
     * @return array
     */
    public function findIdsByName($name)
    {
        $query = $this->getAdapter()->select();
        $query->from($this->_name, array('id'));
        $query->where('name like ?', $name . '%');
        $result = $this->getAdapter()->fetchCol($query);
        return $result;
    }

    /**
     * Fetch all manufacturers for dropdown
     * @return array 
     */
    public function fetchAllForDropdown()
    {
        // fetch all ID and names from database
        $query = $this->getAdapter()->select();
        $query->from($this->_name, array('id', 'name'));
        $query->order('name');
        $result = $this->getAdapter()->fetchPairs($query);
        return $result;
    }

}
